package com.zhuss.core.processor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.wrongWord.SearchWrongWordBo;
import com.zhuss.bean.vo.core.wrongWord.ListWrongWordVo;
import com.zhuss.common.ListVo;
import com.zhuss.common.PageVo;
import com.zhuss.core.db.dao.ext.WrongWordDao;
import com.zhuss.core.db.entity.WrongWord;
import com.zhuss.exception.WpsException;
import com.zhuss.exception.ExceptionCode;


/**
 * @description: 敏感字读
 * @author: zhushanshuo
 * @create: Fri Nov 20 13:10:22 CST 2020
 **/
@Component("wrongWordRead")
public class WrongWordRead {
	
	@Autowired
	private WrongWordDao wrongWordDao;
	
	
	public List<String> getAllName(Integer area) {
		return wrongWordDao.getAllName(area);
	}
	
	
	public Map<String, WrongWord> listMap(Integer area) {
		List<WrongWord> list = wrongWordDao.listAll(area);
		Map<String, WrongWord> map = new HashMap<>();
		for (WrongWord c : list) {
			map.put(c.getName(), c);
		}
		return map;
	}
	
	/**
	 * 敏感字
	 * @return
	 */
	public Object listWrongWord(Integer area) {
		List<WrongWord> wrongWordList = wrongWordDao.listAll(area);
		List<ListVo> result = new ArrayList<>();
		transferList(wrongWordList, result);
		return result;
	}
	
	
	/**
	 * 敏感字
	 * @return
	 */
	public Object listWrongWord(List<Long> wrongWordIdList) {
		List<WrongWord> result = wrongWordDao.listById(wrongWordIdList);
		return result;
	}
	
	
	/**
	 * 根据名字查询敏感字
	 * @param 
	 * @return
	 */
	public List<WrongWord> list(String name) {
		List<WrongWord> result = wrongWordDao.listByName(name);
		return result;
	}
	
	public List<WrongWord> listNotId(String name, Long wrongWordId) {
		List<WrongWord> result = new ArrayList<WrongWord>();
		List<WrongWord> list = list(name);
		for (WrongWord c : list) {
			if (!c.getWrongWordId().equals(wrongWordId)) {
				result.add(c);
				break;
			}
		}
		return result;
	}
	
	/**
	 * 敏感字 
	 * @return
	 */
	public Object listWrongWordId(Integer area) {
		List<WrongWord> wrongWordList = wrongWordDao.listAll(area);
		List<Long> result = new ArrayList<>();
		transferListId(wrongWordList, result);
		return result;
	}
	
	private void transferListId(List<WrongWord> wrongWordList, List<Long> result) {
		for (WrongWord item : wrongWordList) {
			result.add(item.getWrongWordId());
		}
	}

	private void transferList(List<WrongWord> wrongWordList, List<ListVo> result) {
		for (WrongWord item : wrongWordList) {
			ListVo vo = new ListVo();
			vo.setKey(item.getWrongWordId());
			vo.setLabel(item.getName());
			result.add(vo);
		}
		
	}

	/**
	 * 敏感字列表
	 * @param bo
	 * @return
	 */
	public Object list(SearchWrongWordBo bo) {
		PageVo<ListWrongWordVo> result = new PageVo<ListWrongWordVo>();
        result.setPageNum(bo.getPageNum());
        result.setPageSize(bo.getPageSize());
        result.setItems(wrongWordDao.list(bo));
        result.setTotal(wrongWordDao.countList(bo));
		return result;
	}
	
	/**
	 * 敏感字详情
	 * @param wrongWordId
	 * @return
	 * @throws WpsException
	 */
	public Object detail(Long wrongWordId) throws WpsException {
		Object result = wrongWordDao.detail(wrongWordId);
		if (result == null) {
			throw new WpsException(ExceptionCode.数据不存在);
		}
		return result;
	}
	
	

}
